{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "#电商用户行为分析\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 23291027 entries, 0 to 23291026\n",
      "Data columns (total 6 columns):\n",
      " #   Column         Dtype \n",
      "---  ------         ----- \n",
      " 0   user_id        int64 \n",
      " 1   item_id        int64 \n",
      " 2   behavior_type  int64 \n",
      " 3   user_geohash   object\n",
      " 4   item_category  int64 \n",
      " 5   time           object\n",
      "dtypes: int64(4), object(2)\n",
      "memory usage: 1.0+ GB\n",
      "None\n",
      "            user_id       item_id  behavior_type  item_category\n",
      "count  2.329103e+07  2.329103e+07   2.329103e+07   2.329103e+07\n",
      "mean   7.006868e+07  2.023214e+08   1.106268e+00   6.835397e+03\n",
      "std    4.569072e+07  1.167440e+08   4.599087e-01   3.812873e+03\n",
      "min    4.920000e+02  3.700000e+01   1.000000e+00   2.000000e+00\n",
      "25%    3.019541e+07  1.014417e+08   1.000000e+00   3.690000e+03\n",
      "50%    5.626942e+07  2.022430e+08   1.000000e+00   6.054000e+03\n",
      "75%    1.166482e+08  3.035325e+08   1.000000e+00   1.027100e+04\n",
      "max    1.424430e+08  4.045625e+08   4.000000e+00   1.408000e+04\n",
      "    user_id    item_id  behavior_type user_geohash  item_category  \\\n",
      "0  10001082  285259775              1      97lk14c           4076   \n",
      "1  10001082    4368907              1          NaN           5503   \n",
      "2  10001082    4368907              1          NaN           5503   \n",
      "3  10001082   53616768              1          NaN           9762   \n",
      "4  10001082  151466952              1          NaN           5232   \n",
      "\n",
      "            time  \n",
      "0  2014-12-08 18  \n",
      "1  2014-12-12 12  \n",
      "2  2014-12-12 12  \n",
      "3  2014-12-02 15  \n",
      "4  2014-12-12 11  \n"
     ]
    }
   ],
   "source": [
    "#读取数据\n",
    "import os\n",
    "path = os.getcwd() + r'/原始数据'\n",
    "user = pd.read_csv(path + r'/tianchi_fresh_comp_train_user.csv')\n",
    "print(user.info())\n",
    "print(user.describe())\n",
    "print(user.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "user_id                 0\n",
       "item_id                 0\n",
       "behavior_type           0\n",
       "user_geohash     15911010\n",
       "item_category           0\n",
       "time                    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#统计缺失值\n",
    "user.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除重复值\n",
    "user.drop_duplicates(keep='last',inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "user['time'] = pd.to_datetime(user['time'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#提取日期和时间\n",
    "user['dates'] = user.time.dt.date\n",
    "user['month'] = user.dates.values.astype('datetime64[M]')\n",
    "user['hours'] = user.time.dt.hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#转换数据类型\n",
    "user['behavior_type'] = user['behavior_type'].apply(str)\n",
    "user['user_id'] = user['user_id'].apply(str)\n",
    "user['item_id'] = user['item_id'].apply(str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "#可视化分析\n",
    "#每日pv\n",
    "pv_day = user[user['behavior_type']=='1'].groupby('dates')['behavior_type'].count()\n",
    "#每日uv\n",
    "uv_day = user[user['behavior_type']=='1'].drop_duplicates(['user_id','dates']).groupby('dates')['user_id'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": "new Promise(function(resolve, reject) {\n    var script = document.createElement(\"script\");\n    script.onload = resolve;\n    script.onerror = reject;\n    script.src = \"https://assets.pyecharts.org/assets/echarts.min.js\";\n    document.head.appendChild(script);\n}).then(() => {\n\n});",
      "text/plain": [
       "<pyecharts.render.display.Javascript at 0x87cda208>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#分析每日pv和uv趋势\n",
    "from pyecharts.globals import CurrentConfig, NotebookType\n",
    "CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB\n",
    "import pyecharts.options as opts\n",
    "from pyecharts.charts import Line\n",
    "from pyecharts.charts import Grid\n",
    "#作出每日pv和uv趋势图\n",
    "attr = list(pv_day.index)\n",
    "pvuv = (\n",
    "        Line(init_opts=opts.InitOpts(width='1000px',height='500px'))\n",
    "        .add_xaxis(xaxis_data=attr)\n",
    "        .add_yaxis(\n",
    "            'pv',\n",
    "            np.around(pv_day.values/10000,decimals=2),\n",
    "            label_opts=opts.LabelOpts(is_show=False)\n",
    "        )\n",
    "        .add_yaxis(\n",
    "            series_name='uv',\n",
    "            yaxis_index=1,\n",
    "            y_axis=np.around(uv_day.values/10000,decimals=2),\n",
    "            label_opts=opts.LabelOpts(is_show=False)\n",
    "        )\n",
    "        .extend_axis(\n",
    "            yaxis=opts.AxisOpts(\n",
    "                name='uv',\n",
    "                type_='value',\n",
    "                min_=0,\n",
    "                max_=1.6,\n",
    "                interval=0.4,\n",
    "                axislabel_opts=opts.LabelOpts(formatter='{value} 万人')\n",
    "            )\n",
    "        )\n",
    "        .set_global_opts(\n",
    "            tooltip_opts=opts.TooltipOpts(\n",
    "                is_show=True,trigger='axis',axis_pointer_type='cross'\n",
    "            ),\n",
    "            xaxis_opts=opts.AxisOpts(\n",
    "                type_='category',\n",
    "                axispointer_opts=opts.AxisPointerOpts(is_show=True,type_='shadow')\n",
    "            ),\n",
    "            yaxis_opts=opts.AxisOpts(\n",
    "                name='uv',\n",
    "                type_='value',\n",
    "                min_=0,\n",
    "                max_=100,\n",
    "                interval=20,\n",
    "                axislabel_opts=opts.LabelOpts(formatter='{value} 万次'),\n",
    "                axistick_opts=opts.AxisTickOpts(is_show=True),\n",
    "                splitline_opts=opts.SplitLineOpts(is_show=True)\n",
    "            ),\n",
    "            title_opts=opts.TitleOpts(title='pv与uv趋势图')\n",
    "        )\n",
    ")\n",
    "pvuv.load_javascript()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pvuv.render_notebook()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "e94dd2023f21a93510fb04e380baf504272aa09c40641b7e8c2a7346f5fbf161"
  },
  "kernelspec": {
   "display_name": "Python 3.6.2 64-bit",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
